package spring.cloud.tasks.common.nodepaths;

import com.alibaba.fastjson2.JSON;
import spring.cloud.tasks.common.Symbols;
import spring.cloud.tasks.common.curator.Curator;
import spring.cloud.tasks.common.task.TaskItemExecuteRenewalInfo;
import spring.cloud.tasks.common.task.TaskItemRenewalNodeInfo;

import java.util.ArrayList;
import java.util.List;

public final class RenewalNodePaths {
    public static final String ROOT = Symbols.SLASH + "renewals";

    public static String getRoot() {
        return ROOT;
    }

    public static String getTaskNodePath(final String taskId) {
        return getRoot() + Symbols.SLASH + taskId;
    }

    public static String getTaskAndTaskInstanceNodePath(final String taskId, final String taskInstanceId) {
        return getTaskNodePath(taskId) + Symbols.SLASH + taskInstanceId;
    }

    public static String getTaskAndTaskInstanceAndTaskStageNodePath(final String taskId, final String taskInstanceId, String taskStage) {
        return getTaskAndTaskInstanceNodePath(taskId, taskInstanceId) + Symbols.SLASH + taskStage;
    }

    public static String getTaskAndTaskInstanceAndTaskStageAndTaskStage$typeNodePath(final String taskId, final String taskInstanceId, String taskStage, String taskStage$type) {
        return getTaskAndTaskInstanceAndTaskStageNodePath(taskId, taskInstanceId, taskStage) + Symbols.SLASH + taskStage$type;
    }

    public static String getTaskAndTaskInstanceAndTaskStageAndTaskStage$typeAndTaskStage$type$indexNodePath(final String taskId, final String taskInstanceId, String taskStage, String taskStage$type, String taskStage$type$index) {
        return getTaskAndTaskInstanceAndTaskStageAndTaskStage$typeNodePath(taskId, taskInstanceId, taskStage, taskStage$type) + Symbols.SLASH + taskStage$type$index;
    }

    //
    public static List<TaskItemRenewalNodeInfo> getTaskItemRenewalNodeInfoList(Curator curator, String taskId) {
        List<TaskItemRenewalNodeInfo> list = new ArrayList<TaskItemRenewalNodeInfo>();
        List<String> taskInstanceIdList = curator.getChildren(getTaskNodePath(taskId));
        if (taskInstanceIdList != null) {
            for (String taskInstanceId : taskInstanceIdList) {
                List<String> taskStageList = curator.getChildren(getTaskAndTaskInstanceNodePath(taskId, taskInstanceId));
                if (taskStageList != null) {
                    for (String taskStage : taskStageList) {
                        List<String> taskStage$typeList = curator.getChildren(getTaskAndTaskInstanceAndTaskStageNodePath(taskId, taskInstanceId, taskStage));
                        if (taskStage$typeList != null) {
                            for (String taskStage$type : taskStage$typeList) {
                                List<String> taskItemIndexList = curator.getChildren(getTaskAndTaskInstanceAndTaskStageAndTaskStage$typeNodePath(taskId, taskInstanceId, taskStage, taskStage$type));
                                if (taskItemIndexList != null) {
                                    for (String taskItemIndex : taskItemIndexList) {
                                        TaskItemRenewalNodeInfo taskInstanceAndTaskItem = new TaskItemRenewalNodeInfo();
                                        taskInstanceAndTaskItem.setTaskId(taskId);
                                        taskInstanceAndTaskItem.setTaskInstanceId(taskInstanceId);
                                        taskInstanceAndTaskItem.setTaskStage(taskStage);
                                        taskInstanceAndTaskItem.setTaskStage$type(taskStage$type);
                                        taskInstanceAndTaskItem.setTaskStage$type$index(taskItemIndex);
                                        list.add(taskInstanceAndTaskItem);
                                    }

                                }
                            }
                        }

                    }

                }
            }

        }
        return list;
    }

    public static TaskItemExecuteRenewalInfo getTaskItemExecuteRenewalInfo(Curator curator, TaskItemRenewalNodeInfo taskItemRenewalNodeInfo) {
        String nodePath = getTaskAndTaskInstanceAndTaskStageAndTaskStage$typeAndTaskStage$type$indexNodePath(taskItemRenewalNodeInfo.getTaskId(), taskItemRenewalNodeInfo.getTaskInstanceId(), taskItemRenewalNodeInfo.getTaskStage(), taskItemRenewalNodeInfo.getTaskStage$type(), taskItemRenewalNodeInfo.getTaskStage$type$index());
        if (!curator.checkExists(nodePath)) {
            return null;
        } else {
            String data = curator.getData(nodePath);
            TaskItemExecuteRenewalInfo taskItemExecuteRenewalInfo = JSON.parseObject(data, TaskItemExecuteRenewalInfo.class);
            return taskItemExecuteRenewalInfo;
        }
    }

    public static TaskItemExecuteRenewalInfo getTaskItemExecuteRenewalInfo(Curator curator, String taskItemRenewalNodeInfoNodePath) {
        if (!curator.checkExists(taskItemRenewalNodeInfoNodePath)) {
            return null;
        } else {
            String data = curator.getData(taskItemRenewalNodeInfoNodePath);
            TaskItemExecuteRenewalInfo taskItemExecuteRenewalInfo = JSON.parseObject(data, TaskItemExecuteRenewalInfo.class);
            return taskItemExecuteRenewalInfo;
        }
    }

}
